//
// This example shows how to scale the Anted.Server.
//
// You must specify the `presence` option on Anted.Server when using multiple
// processes. This example uses Redis as presence server.
//
import * as http from 'http';
import * as Koa from 'koa';
import * as bodyParser from 'koa-bodyparser';
import * as Router from 'koa-router';
import * as koaStatic from 'koa-static';
// import * as WebSocket from "uws";

import { Server } from '../src/Server';
import { RedisPresence } from './../src/presence/RedisPresence';
import { ChatRoom } from './ChatRoom';

const port = Number(process.env.PORT || 2657);
const endpoint = 'localhost';

const app = new Koa();

// Create HTTP & WebSocket servers
const server = http.createServer(app.callback());
const gameServer = new Server({
  verifyClient: (info, next) => {
    console.log('custom verifyClient!');
    next(true);
  },
  presence: new RedisPresence(),
  // engine: WebSocket.Server,
  server,
});

// Register ChatRoom as "chat"
gameServer.register('chat', ChatRoom).
  // demonstrating public events.
  on('create', (room) => console.log('handler: room created!', room.roomId)).
  on('join', (room, client) => console.log('handler: client', client.sessionId, 'joined', room.roomId)).
  on('leave', (room, client) => console.log('handler: client', client.sessionId, 'left', room.roomId)).
  on('dispose', (room) => console.log('handler: room disposed!', room.roomId));

app.use(koaStatic(__dirname));
app.use(bodyParser());

gameServer.listen(port);

console.log(`Listening on http://localhost:${port}`);
